﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Merge
{
    public class Tidyer
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private IList<TidyRule> tidyRules = new List<TidyRule>();
        public Tidyer(string namesTidyConfigFile)
        {
            if (namesTidyConfigFile != null && File.Exists(namesTidyConfigFile))
            {
                log.Info($"读取规范词汇配置{namesTidyConfigFile}");
                this.tidyRules = File.ReadAllLines(namesTidyConfigFile, Encoding.UTF8)
                    .Select(line => TidyRule.FromConfigLine(line))
                    .Where(r => r != null)
                    .ToList();
                log.Info(this.ToString());
            }


        }

        public Tidyer(IList<TidyRule> tidyRules)
        {
            this.tidyRules = tidyRules;
        }

        public string TidyName(string name)
        {
            foreach (var rule in tidyRules)
            {
                foreach (var source in rule.Sources)
                {
                    name = name.Replace(source, rule.To);
                }
            }
            return name;
        }


        public override string ToString()
        {
            return string.Join(Environment.NewLine, this.tidyRules.Select(x => x.ToString()));
        }
    }
}
